﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Directory.Web
{
    public partial class Admin_RecommandCategory : System.Web.UI.Page
    {
        protected string categoryIdKey = "categoryId";
        protected int categoryId = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            int.TryParse(Request.QueryString["categoryId"], out categoryId);
            if (!IsPostBack)
            {
                try
                {
                    if (Request.QueryString["action"].ToLower().Trim() == "unrecommand")
                    {
                        if (int.TryParse(Request.QueryString["categoryId"], out categoryId))
                        {
                            try
                            {
                                CategoryDataProxy.UnrecommandCategory(categoryId);
                                ClientScript.RegisterClientScriptBlock(this.GetType(), null, "<script>alert('操作成功。');</script>");
                            }
                            catch
                            {
                                ClientScript.RegisterClientScriptBlock(this.GetType(), null, "<script>alert('操作失败：服务器错误。');</script>");
                            }
                        }
                        else
                        {
                            ClientScript.RegisterClientScriptBlock(this.GetType(), null, "<script>alert('操作失败：参数不正确。');</script>");
                        }
                    }
                }
                catch
                {
                }
                int totalRecords;
                rptCateogries.DataSource = CategoryDataProxy.GetRecommandCategories(0, 999, out totalRecords);
                rptCateogries.DataBind();

                CascadingCategory.DataSource = CategoryDataProxy.GetChildsOfCategory(WebSettings.GetInstance().RootCategoryId);
                int cid;
                if (int.TryParse(Request.QueryString["categoryId"], out cid))
                {
                    IList<Directory.Model.CategoryInfo> parentPath = CategoryDataProxy.GetRecursiveParentsOfCategory(cid);
                    string pathStr = null;
                    foreach (Directory.Model.CategoryInfo cat in parentPath)
                    {
                        pathStr += cat.CategoryId.ToString() + ",";
                    }
                    if (pathStr != null)
                    {
                        CascadingCategory.SelectedIndexStr = pathStr.Remove(pathStr.Length - 1);
                        CascadingCategory.AutoLoad = true;
                    }
                }
                CascadingCategory.DataBind();
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (CascadingCategory.SelectedId > 0)
            {
                try
                {
                    CategoryDataProxy.RecommandCategory(CascadingCategory.SelectedId);
                    ClientScript.RegisterClientScriptBlock(this.GetType(), null, "<script>alert('操作成功');window.location.href='RecommandCategory.aspx';</script>");
                }
                catch
                {
                    ClientScript.RegisterClientScriptBlock(this.GetType(), null, "<script>alert('操作失败：服务器错误。');</script>");
                }
            }
            else
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(), null, "<script>alert('未选择" + Resources.Resource.ResourceManager.GetString("Category") + "');</script>");
            }
        }
    }
}
